package com.shijunhao.graduation.exception.handler;

import com.shijunhao.graduation.domain.bean.ResultCode;
import com.shijunhao.graduation.exception.BaseException;
import com.shijunhao.graduation.utils.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author: shijunhao
 * @date: 2022/3/2 22:45
 * @description: 全局异常处理器
 */
@ControllerAdvice
@ResponseBody
public class AllExceptionHandler{

    private final Logger logger = LoggerFactory.getLogger(AllExceptionHandler.class);

    @ExceptionHandler
    public ResultCode<?> handlerBaseException(BaseException e){
        logger.error("业务异常 {}", ExceptionUtils.printBusinessStackTrace(e,"com.shijunhao.graduation"));
        return ResultCode.resultCode(e.getCode(),e.getMsg());
    }
}
